package com.example.enterprise.vm

import com.example.common.view.vm.BaseViewModel
import com.example.enterprise.api.EnterpriseApiService
import com.example.enterprise.entity.EnterpriseEntityItem
import com.example.enterprise.model.EnterpriseRepository
import com.example.enterprise.model.EnterpriseResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class EnterpriseViewModel@Inject constructor(private val apiService: EnterpriseApiService): BaseViewModel<EnterpriseRepository, EnterpriseResultData<List<EnterpriseEntityItem>>>() {
    fun getEnterprise(){
        mViewModelScope.launch (mDispatcher){
            try {
                val result = apiService.getEnterprise()
                val res = EnterpriseResultData(
                    data = result.data,
                    state = result.state,
                    msg = result.msg
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getEnterpriseById(id:Int){
        mViewModelScope.launch (mDispatcher){
            try {
                val result =apiService.getEnterpriseById(id)
                val res =EnterpriseResultData(
                    data = listOf(result.data),
                    state = result.state,
                    msg= result.msg
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}